    /*  
    championBuilder - Champions Online character planner.
    Copyright (C) 2009 Moritz Hartmeier
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You can find the GNU General Public License in the data folder.
    */


package ch.zarzu.champions.builder.gui;

import java.awt.*;
import java.util.HashMap;
import java.util.LinkedList;

import javax.swing.*;

import ch.zarzu.champions.builder.*;
import ch.zarzu.champions.builder.logic.CharacteristicObserver;
import ch.zarzu.champions.builder.logic.ValueCalculator;

public class SubCharacteristic extends JLayeredPane implements Characteristic {
    private String formula, concat, unit;
    private JLabel label, value_label, unit_label;
    private boolean is_stat;
    private HashMap<String, String> attr_map;
    //these are not children in the sense of subcharacteristics, 
    //but in the sense of how fire resistance and cold resistance are children of elemental resistance
    private LinkedList<String> children; 
    
    public SubCharacteristic (HashMap<String, String> map) {
        is_stat = map.containsKey("stat");
        formula = map.get("formula");
        concat = map.get("concat");
        unit = map.get("unit");
        children = new LinkedList<String>();
        if(map.containsKey("children")) {
            for(String c: map.get("children").split("[,]"))
                children.add(c);
        }
        attr_map = map;
        setName(map.get("name"));
        
        CharacteristicObserver.getInstance().subscribe(this);
        
        draw();
    }
    
    private void draw() {
        SpringLayout layout = new SpringLayout();
        this.setLayout(layout);
        
        label = new JLabel(SystemLink.getInstance().translateCapitalized(getName()).replace("Resistance", "Res").replace("Strength", "Str"));
        label.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
        label.setForeground(Color.WHITE);
        add(label);
        
        value_label = new JLabel("");
        value_label.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
        value_label.setForeground(Color.WHITE);
        add(value_label);
        
        unit_label = new JLabel(unit);
        unit_label.setFont(new Font("Comic Sans MS", Font.PLAIN, 11));
        unit_label.setForeground(Color.WHITE);
        add(unit_label);
        
        layout.putConstraint(SpringLayout.WEST, label, 8, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.EAST, value_label, 215, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.WEST, unit_label, 4, SpringLayout.EAST, value_label);
        layout.putConstraint(SpringLayout.SOUTH, this, 0, SpringLayout.SOUTH, label);
    }
    
    public void setValue(Double value) {
        value_label.setText(Long.toString(Math.round(value)));
    }
    
    public void setTooltip(HashMap<String, Double> map) {
        String tooltip = "<html><span style='font-family:Comic Sans MS;font-size:90%;color:white'>";
        
        if(attr_map.containsKey("info")) {
            ValueCalculator calculator = new ValueCalculator(attr_map.get("infoformula"));
            tooltip += "<b>" + attr_map.get("info") + ": " + calculator.compute().intValue() + "" + attr_map.get("infounit") + "</b><br/><br/>";
        }
        
        //the base value is always on top
        if(map.containsKey("Base")) {
            Double value = new Double(Math.round(map.get("Base")*10));
            tooltip += "Base (" + value/10 + ")<br/>";
        }
        for(String s : map.keySet()) {
            if(!s.equals("Base")) {
                Double value = new Double(Math.round(map.get(s)*10));
                tooltip += s + " (" + value/10 + ")<br/>";
            }
        }
        tooltip += "</span></html>";
        this.setToolTipText(tooltip);
    }
    
    public void clearTooltip() {
        this.setToolTipText("");
    }

    public String getFormula() {
        return formula;
    }
    
    public LinkedList<String> getChildren() {
        return children;
    }
    
    public String getConcat() {
        return concat;
    }
    
    public boolean isStat() {
        return is_stat;
    }
    
    public void updateLanguage() {
        if(!getName().equals("separator"))
            label.setText(SystemLink.getInstance().translateCapitalized(getName()).replace("Resistance", "Res").replace("Strength", "Str"));
    }
}
